package com.example.imtoken.modules.main.wallet.manage.admin.details

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema

class AdminDetailsViewModel : BaseViewModel() {

    var accountSchema: AccountSchema? = null

    val id = MutableLiveData<String>()
    val account = MutableLiveData<String>()

    fun getDefAccount() {
        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLogin.walletAddress)
        accountSchema = account
        id.value = account.token
        this.account.value = account.account
    }

    fun getDefAccount(key: String?) {
        val account = AppDataBase.instance.getAccountDao().getDefAccount(key)
        accountSchema = account
        id.value = account.token
        this.account.value = account.account
    }

    fun updateAccountName(account: String?) {
        val adminList = AppDataBase.instance.getAccountDao().findAdminAccount()
        adminList.forEach {
            it.account = account
            if (AppDataBase.instance.getAccountDao().updateAccount(it) > 0) {
                LogUtils.e(it.token, "修改身份名成功")
            } else {
                LogUtils.e(it.token, "修改身份名失败")
            }
        }
        getDefAccount()
//        accountSchema?.let {
//            it.account = account
//            if (AppDataBase.instance.getAccountDao().updateAccount(it) > 0) {
//                LogUtils.e("修改身份名成功")
//                this.account.value = account
//            } else {
//                LogUtils.e("修改身份名失败")
//            }
//        }
    }

}